/**
 * Created by lihao on 2017/6/28.
 */
const keystone = require('keystone');
const Promise = require('bluebird');
const _ = require('lodash');
const Post = keystone.list('Authority');
const async = require('async');
exports = module.exports = function (req, res) {
	let reqQuery = _.omit(_.assign(req.query, req.body), ['pageNumber', 'pageSize', 'maxPages']);
	let filters = {
		status: {'$nin': ['delete']}
	};
	let keyword = ['name'];
	let filterField = [];
	//时间类字段操作
	//'endDate' 结束时间
	//'range' gt,gte,lt,lte,eq，默认 get
	let omitFueld = ['__v'];
	let optionalExpression = _.transform(omitFueld, function (r, val) {
		r[val] = 0;
	}, {});
	//筛选数据
	// _.map(reqQuery, function (val, key) {
	// 	if (val != '' && _.indexOf(filterField, key) >= 0) {
	// 		if (/Time/.test(key)) {
	// 			let queryTime = {};
	// 			let range = 'gte';
	// 			if (_.has(reqQuery, key + 'EndDate')) {
	// 				queryTime = _.set(queryTime, '$lte', new Date(_.get(reqQuery, key + 'EndDate')));
	// 			} else if (_.has(reqQuery, 'range')) {
	// 				range = reqQuery.range;
	// 			}
	// 			queryTime = _.set(queryTime, '$' + range, new Date(val));
	// 			filters = _.set(filters, key, queryTime);
	// 		} else if (val != '') {
	// 			filters = _.set(filters, key, new RegExp(val));
	// 		} else {
    //
	// 		}
	// 	} else if (val != '' && _.indexOf(keyword, key) >= 0) {
	// 		var keywordFilter = _.map(keyword, function (item) {
	// 			return _.set({}, item, new RegExp(val));
	// 		});
	// 		filters = _.set(filters, '$or', keywordFilter);
	// 	}
	// });
	// let query = Post.paginate({
	//     page: reqQuery.pageNumber || 1,
	//     perPage: reqQuery.pageSize || 999999,
	//     // maxPages: reqQuery.maxPages || 10,
	//     filters: filters,
	//     optionalExpression: optionalExpression
	// }).populate('children');
	
	let query = Post.model.find({parent: []}).populate('children');
	query.exec(function (err, results) {
		if (err) return res.json({code: 0, message: '请求失败', error: err});
		return res.json({code: 1, message: '请求成功', data: results});
	});
};
